from django.conf import settings
from django.contrib.sites.models import Site
from django.contrib.syndication.feeds import Feed
from django.utils.feedgenerator import Atom1Feed
from django.utils.translation import ugettext_lazy as _
from clapton.apps.blogs.models import Weblog, Entry
from clapton.apps.documents.models import Document
from clapton.apps.events.models import Event
from clapton.apps.photos.models import Photo
from clapton.apps.podcasts.models import Channel, Podcast
from clapton.apps.polls.models import Poll
from clapton.apps.stories.models import Section, Story
from clapton.apps.videos.models import Video

site = Site.objects.get_current()

class BlogEntryFeed(Feed):
    def get_object(self, bits):
        if len(bits) < 1:
            raise ObjectDoesNotExist
        return Weblog.objects.get(slug=bits[-1])

    def title(self, obj):
	if obj:
	    return _(u"Blog - %s" % obj.name)
        return _(u'%s Blogs' % site.name)

    def link(self,obj):
	if obj:
	    return obj.get_absolute_url()
	return '/blogs'

    def description(self, obj):
	if obj:
            return obj.description
	return _(u'Blogs on %s' % site.name)

    def item_author_name(self, item):
        return item.user.get_full_name()    

    def items(self, obj):
	if obj:
	    return Entry.published_objects.filter(weblog__site=settings.SITE_ID, weblog=obj).order_by('-pub_date')[:10]
        return Entry.published_objects.filter(weblog__site=settings.SITE_ID).order_by('-pub_date')[:10]

    def item_pubdate(self, item):
	return item.pub_date

class DocumentFeed(Feed):
    title = _(u'%s Documents' % site.name)
    link = '/documents'
    description = _(u'Documents on %s.' % site.name)

    def item_author_name(self, item):
        return item.user.get_full_name()    

    def items(self):
        return Document.published_objects.filter(site=settings.SITE_ID).order_by('-pub_date')[:10]

    def item_pubdate(self, item):
	return item.pub_date    

class EventFeed(Feed):
    title = _(u'%s Events' % site.name)
    link = '/events'
    description = _(u'Events on %s.' % site.name)

    def item_author_name(self, item):
        return item.user.get_full_name()    

    def items(self):
        return Event.published_objects.filter(site=settings.SITE_ID).order_by('-pub_date')[:10]

    def item_pubdate(self, item):
	return item.pub_date
	
class PhotoFeed(Feed):
    title = _(u'%s Photos' % site.name)
    link = '/photos'
    description = _(u'Photos on %s.' % site.name)

    def item_author_name(self, item):
	if (item.photographer):
            return u'%s' % (item.photographer)    
        else:
            return item.user.get_full_name()    

    def items(self):
        return Photo.published_objects.filter(site=settings.SITE_ID).order_by('-pub_date')[:10]

    def item_pubdate(self, item):
	return item.pub_date

class PodcastFeed(Feed):
    def get_object(self, bits):
        if len(bits) < 1:
            raise ObjectDoesNotExist
        return Channel.objects.get(slug=bits[-1])

    def title(self, obj):
	if obj:
	    return _(u"Podcast - %s" % obj.name)
        return _(u'%s Podcasts' % site.name)
	
    def link(self,obj):
	if obj:
	    return obj.get_absolute_url()
	return '/podcasts'
	
    def description(self, obj):
	if obj:
            return obj.description
	return _(u'Podcasts on %s' % site.name)
    
    def item_author_name(self, item):
        return item.user.get_full_name()    

    def items(self, obj):
	if obj:
	    return Podcast.published_objects.filter(channel__site=settings.SITE_ID, channel=obj).order_by('-pub_date')[:10]
        return Podcast.published_objects.filter(channel__site=settings.SITE_ID).order_by('-pub_date')[:10]

    def item_pubdate(self, item):
	return item.pub_date

    def item_enclosure_url(self, item):
        return item.get_audio_url()

    def item_enclosure_length(self, item):
        return item.get_audio_size()

    def item_enclosure_mime_type(self, item):
        """
        Takes an item, as returned by items(), and returns the item's
        enclosure MIME type.
        """

class PollFeed(Feed):
    title = _(u'%s Polls' % site.name)
    link = '/polls'
    description = _(u'Polls on %s.' % site.name)
    
    def item_author_name(self, item):
        return item.user.get_full_name()    

    def items(self):
        return Poll.published_objects.filter(site=settings.SITE_ID).order_by('-pub_date')[:10]

    def item_pubdate(self, item):
	return item.pub_date

class StoryFeed(Feed):
    def get_object(self, bits):
        if len(bits) < 1:
            raise ObjectDoesNotExist
        return Section.objects.get(slug=bits[-1])
	
    def title(self, obj):
	if obj:
	    return _(u"Stories on %s - %s" % (site.name, obj.name))
        return _(u'%s Stories' % site.name)
	
    def link(self,obj):
	if obj:
	    return obj.get_absolute_url()
	return '/stories'
	
    def description(self, obj):
	if obj:
            return obj.description
	return _(u'Stories on %s' % site.name)
	
    def item_author_name(self, item):
        return item.user.get_full_name()    
            
    def items(self, obj):
	if obj:
             return Story.published_objects.filter(assoc_section__site=settings.SITE_ID, assoc_section=obj.slug).order_by('-pub_date')[:10]
        return Story.published_objects.filter(assoc_section__site=settings.SITE_ID).order_by('-pub_date')[:10]

    def item_pubdate(self, item):
        return item.pub_date

class BlogEntryFeedAtom(BlogEntryFeed):
    feed_type = Atom1Feed
    subtitle = BlogEntryFeed.description

class DocumentFeedAtom(DocumentFeed):
    feed_type = Atom1Feed
    subtitle = DocumentFeed.description

class EventFeedAtom(EventFeed):
    feed_type = Atom1Feed
    subtitle = EventFeed.description

class PhotoFeedAtom(PhotoFeed):
    feed_type = Atom1Feed
    subtitle = PhotoFeed.description

class PodcastFeedAtom(PodcastFeed):
    feed_type = Atom1Feed
    subtitle = PodcastFeed.description

class PollFeedAtom(PollFeed):
    feed_type = Atom1Feed
    subtitle = PollFeed.description

class StoryFeedAtom(StoryFeed):
    feed_type = Atom1Feed
    subtitle = StoryFeed.description
